﻿using System.Runtime.Serialization;
using BaseTypesLib.Interfaces;
using System.Xml.Linq;
using System.Linq;

namespace BaseTypesLib.Model
{
    [DataContract(IsReference = true)]
    public class LabelField:Field, IUseAllWidth, IDrawStyle
    {
        private string text;
        private bool useAllWidth;

        public LabelField()
        {
            Focusable = false;
        }
        [DataMember]
        public string Text 
        {
            get { return text; }
            set
            {
                if (text != value)
                {
                    text = value;
                    OnPropertyChanged("Text");
                }
            }
        }
        [DataMember]
        public bool UseAllWidth
        {
            get { return useAllWidth; }
            set
            {
                if (useAllWidth != value)
                {
                    useAllWidth = value;
                    OnPropertyChanged("UseAllWidth");
                }
            }
        }
        protected override XElement GetXmlElement()
        {
            XElement el = base.GetXmlElement();
            if (Text != null && Text.Trim().Length > 0)
                el.SetAttributeValue("Text", Text.Trim());
            if (UseAllWidth)
                el.SetAttributeValue("UseAllWidth", UseAllWidth);

            return el;
        }
        protected override void FromXmlElement(XElement elem)
        {
            base.FromXmlElement(elem);
            var lbl = elem.Attributes("Text").FirstOrDefault();
            if (lbl != null)
                Text = lbl.Value;

            var e = elem.Attributes("UseAllWidth").FirstOrDefault();
            if (e != null) UseAllWidth = (bool)e;
        }
    }
}
